package com.mwcz.hot.task.order;

import com.mwcz.hot.store.service.OrderTaskService;
import com.mwcz.hot.work.dao.WorkDao;
import com.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统自动取消未支付订单task任务
 *
 * @author Mr.Bai
 * @since 2021-06-18
 */
@Component
@Configuration //读取配置
@EnableScheduling // 2.开启定时任务
public class WorkAutoCancelTask {
    //日志
    private static final Logger logger = LoggerFactory.getLogger(WorkAutoCancelTask.class);

    @Autowired
    private WorkDao workDao;

    @Scheduled(fixedDelay = 1000 * 1L) //1s同步一次数据
    public void init() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info("---OrderAutoCancelTask task------produce Data with fixed rate task: Execution Time - {}", DateUtil.nowDateTime());
        try {
            List<Map<String,Object>> list =workDao.selWorKList();
            for (int i =0;i<list.size();i++){
                if((new Date()).getTime() - format.parse(list.get(i).get("create_time").toString().substring(0,list.get(i).get("create_time").toString().indexOf("."))).getTime() > Integer.parseInt(list.get(i).get("work_time").toString())*60*1000){
                    Map<String,Object> carRecord = new HashMap<>();
                    carRecord.put("id",list.get(i).get("car_id"));
                    carRecord.put("status","2");
                    carRecord.put("lat",list.get(i).get("site_lat"));
                    carRecord.put("lng",list.get(i).get("site_lng"));
                    workDao.updateCar(carRecord);
                    Map<String,Object> workRecord = new HashMap<>();
                    workRecord.put("id",list.get(i).get("id"));
                    workRecord.put("status","2");
                    workDao.updateWork(workRecord);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("OrderAutoCancelTask.task" + " | msg : " + e.getMessage());
        }

    }
}
